package fangbing.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import fangbing.model.User;
import fangbing.service.UserService;
import fangbing.util.page.PageBean;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;

	
	@RequestMapping("/user/searchUserList")
	public ModelAndView searchUserList(PageBean<User> poPageBean,User user) {
		
		ModelAndView modelAndView = new ModelAndView("user/userList");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map = userService.searchUserListByPage(poPageBean,user);
		
		modelAndView.addAllObjects(map);
		
		return modelAndView;
	}
	
	
	@RequestMapping("/user/save")
	public ModelAndView addUser(User user) {
		
		ModelAndView modelAndView = new ModelAndView("redirect:/user/searchUserList.do");
		
		userService.addUser(user);
		
		return modelAndView;
	}
	
	@RequestMapping("/user/update")
	public ModelAndView updateUser(User user) {
		
		ModelAndView modelAndView = new ModelAndView("user/userAdd");
		
		userService.updateUser(user);
		
		return modelAndView;
	}
	
	@RequestMapping("/user/delete")
	public ModelAndView deleteUser(User user) {
		
		ModelAndView modelAndView = new ModelAndView("redirect:/user/searchUserList.do");
		
		userService.deleteUser(user);
		
		return modelAndView;
	}
	
	@RequestMapping("/user/toAddOrUpdate")
	public ModelAndView toAddOrUpdateUser(User user) {
		
		ModelAndView modelAndView = new ModelAndView("user/userAdd");
		
		if(null==user.getId()){
			//点击新增按钮
		}else{
			//点击修改按钮
			modelAndView = new ModelAndView("redirect:/user/searchById.do?id="+user.getId());
		}
		return modelAndView;
	}
	
	@RequestMapping("/user/searchById")
	public ModelAndView searchUserById(User user){
		
		ModelAndView modelAndView = new ModelAndView("user/userAdd");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map = userService.searchUserById(user);
		
		modelAndView.addAllObjects(map);
		
		return modelAndView;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	
}
